<template>
  <div class="shopping-cart">
    <h2 class="cart-title">Shopping Cart</h2>
    
    <!-- Empty Cart State -->
    <el-empty v-if="cartItems.length === 0" 
              description="Your cart is empty">
      <el-button type="primary" @click="$router.push('/')">
        Go Shopping
      </el-button>
    </el-empty>

    <!-- Cart Items List -->
    <div v-else>
      <el-table :data="cartItems" style="width: 100%">
        <el-table-column width="180">
          <template #default="scope">
            <img :src="scope.row.cover" class="cart-item-image">
          </template>
        </el-table-column>

        <el-table-column prop="name" label="Product">
          <template #default="scope">
            <h3>{{ scope.row.name }}</h3>
            <p class="text-muted">{{ scope.row.brand }} - {{ scope.row.weight }}g</p>
          </template>
        </el-table-column>

        <el-table-column label="Quantity" width="200">
          <template #default="scope">
            <el-input-number 
              v-model="scope.row.quantity" 
              :min="1" 
              :max="99"
              @change="updateQuantity(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column prop="price" label="Price" width="120">
          <template #default="scope">
            <span class="price">${{ scope.row.price }}</span>
          </template>
        </el-table-column>

        <el-table-column label="Subtotal" width="120">
          <template #default="scope">
            <span class="price">${{ (scope.row.price * scope.row.quantity).toFixed(2) }}</span>
          </template>
        </el-table-column>

        <el-table-column width="100">
          <template #default="scope">
            <el-button type="danger" @click="removeItem(scope.row)" icon="Delete" circle />
          </template>
        </el-table-column>
      </el-table>

      <!-- Cart Summary -->
      <div class="cart-summary">
        <div class="summary-item">
          <span>Subtotal:</span>
          <span class="price">${{ subtotal.toFixed(2) }}</span>
        </div>
        <div class="summary-item">
          <span>Shipping:</span>
          <span class="price">${{ shipping.toFixed(2) }}</span>
        </div>
        <div class="summary-item total">
          <span>Total:</span>
          <span class="price">${{ total.toFixed(2) }}</span>
        </div>
        <el-button type="primary" size="large" @click="checkout">
          Proceed to Checkout
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'ShoppingCart',
  data() {
    return {
      shipping: 5.99
    }
  },
  computed: {
    ...mapState(['cartItems']),
    subtotal() {
      return this.cartItems.reduce((sum, item) => {
        return sum + (item.price * item.quantity)
      }, 0)
    },
    total() {
      return this.subtotal + this.shipping
    }
  },
  methods: {
    updateQuantity(item) {
      this.$store.commit('updateCartItemQuantity', {
        id: item.id,
        quantity: item.quantity
      })
    },
    removeItem(item) {
      this.$confirm('Remove this item from cart?', 'Confirm', {
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
        type: 'warning'
      }).then(() => {
        this.$store.commit('removeFromCart', item.id)
        this.$message.success('Item removed successfully')
      }).catch(() => {})
    },
    checkout() {
      // 这里添加结账逻辑
      this.$message.info('Checkout functionality coming soon!')
    }
  }
}
</script>

<style scoped>
.shopping-cart {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.cart-title {
  margin-bottom: 30px;
  font-size: 24px;
  color: #333;
}

.cart-item-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
}

.text-muted {
  color: #909399;
  font-size: 14px;
  margin-top: 5px;
}

.price {
  color: #f56c6c;
  font-weight: bold;
}

.cart-summary {
  margin-top: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  width: 300px;
  margin-left: auto;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 16px;
}

.total {
  border-top: 1px solid #dcdfe6;
  padding-top: 15px;
  font-size: 18px;
  font-weight: bold;
}

.el-button--large {
  width: 100%;
  margin-top: 20px;
}
</style>